package xuegao.study.mall.rbac.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.api.CommonPage;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.model.ums.UmsResource;
import xuegao.study.mall.common.model.ums.UmsResourceCategory;
import xuegao.study.mall.rbac.service.UmsResourceCategoryService;
import xuegao.study.mall.rbac.service.UmsResourceService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "UmsResourceController")
@Tag(name = "UmsResourceController", description = "资源管理相关接口")
@RestController
@RequestMapping("/resource")
public class UmsResourceController {

    @Autowired
    private UmsResourceService umsResourceService;

    @Autowired
    private UmsResourceCategoryService umsResourceCategoryService;

    @ApiOperation("添加资源")
    @PostMapping("/create")
    public CommonResult<Integer> add(@RequestBody UmsResource umsResource) {
        int count = umsResourceService.insert(umsResource);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除资源")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Integer> deleteResource(@PathVariable("id") Long id) {
        int count = umsResourceService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("更新资源")
    @PostMapping("/update/{id}")
    public CommonResult<Integer> update(@PathVariable("id") Long id, @RequestBody UmsResource umsResource) {
        int count = umsResourceService.update(id, umsResource);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("查询所有资源")
    @GetMapping("/listAll")
    public CommonResult<List<UmsResource>> listAll() {
        List<UmsResource> list = umsResourceService.listAll();
        return CommonResult.success(list);
    }

    @ApiOperation("分页查询资源")
    @GetMapping("/list")
    public CommonResult<CommonPage<UmsResource>> listResource(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "nameKeyword", defaultValue = "") String nameKeyword,
            @RequestParam(value = "urlKeyword", defaultValue = "") String urlKeyword,
            @RequestParam(value = "categoryId", required = false) Long categoryId) {
        Map<String, Object> params = new HashMap<>();
        if (!StringUtils.isEmpty(nameKeyword)) {
            params.put("name", nameKeyword);
        }
        if (!StringUtils.isEmpty(urlKeyword)) {
            params.put("url", urlKeyword);
        }
        if (null != categoryId) {
            params.put("categoryId", categoryId);
        }
        List<UmsResource> list = umsResourceService.list(pageNum, pageSize, params);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("查询资源目录")
    @GetMapping("/resourceCategory/listAll")
    public CommonResult<List<UmsResourceCategory>> listMenu(){
        List<UmsResourceCategory> list = umsResourceCategoryService.listMenu();
        return CommonResult.success(list);
    }

}
